<template>
  <el-card class="box-card" style="margin: 10px 0">
    <!-- 头部 -->
    <div slot="header" class="clearfix">
      <!--   头部左侧内容     @tab-click="handleClick"-->
      <el-tabs class="tab" v-model="activeName">
        <el-tab-pane label="销售额" name="sale"></el-tab-pane>
        <el-tab-pane label="访问量" name="visite"></el-tab-pane>
      </el-tabs>
      <!--  头部右侧内容   -->
      <div class="right">
        <span @click="setDay">今日</span>
        <span @click="setWeek">本周</span>
        <span @click="setMonth">本月</span>
        <span @click="setYear">本年</span>
        <!-- v-model="value1" -->
        <el-date-picker
            v-model="date"
            value-format="yyyy-MM-dd"
            class="date"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="mini"
        >
        </el-date-picker>
      </div>

    </div>
    <!-- 身体 -->
    <div>
      <el-row :gutter="10">
        <el-col :span="19">
          <!-- 容器 -->
          <div class="charts" ref="charts"></div>
        </el-col>
        <el-col :span="5">
          <h3>门店{{ title }}排名</h3>
          <ul>
            <div v-if="activeName === 'sale'">
              <li v-for="item in listState.orderRank" :key="item.no">
                <span class="rindex">{{ item.no }}</span>
                <span>{{ item.name }}</span>
                <span class="rvalue">{{ item.money }}</span>
              </li>
            </div>
            <div v-else>
              <li v-for="item in listState.userRank" :key="item.no">
                <span class="rindex">{{ item.no }}</span>
                <span>{{ item.name }}</span>
                <span class="rvalue">{{ item.money }}</span>
              </li>
            </div>
          </ul>
        </el-col>
      </el-row>
    </div>

  </el-card>
</template>

<script>

import * as echarts from 'echarts'
import dayjs from 'dayjs'
import {mapState} from 'vuex'

export default {
  name: 'Sale',
  data() {
    return {
      activeName: 'sale',
      myCharts: null,
      // 收集日历数据
      date: []
    }
  },
  computed: {
    title() {
      return this.activeName === 'sale' ? '销售额' : '访问量'
    },
    ...mapState({
      listState: state => state.home.list
    })
  },
  watch: {
    // 重新修改图标配置数据
    title() {
      this.myCharts.setOption({
        title: {
          text: this.title
        },
        xAxis: {
          data: this.title === '销售额' ? this.listState.orderFullYearAxis : this.listState.userFullYearAxis
        },
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '40%',
            data: this.title === '销售额' ? this.listState.orderFullYear : this.listState.userFullYear
          }
        ]
      })
    },
    listState() {
      this.myCharts.setOption({
        title: {
          text: this.title + '趋势'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.listState.orderFullYearAxis,
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '40%',
            data: this.listState.orderFullYear
          }
        ]
      })
    }
  },
  mounted() {
    // 初始化echarts
    this.myCharts = echarts.init(this.$refs.charts)
    this.myCharts.setOption({
      title: {
        text: this.title + '趋势'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: [
        {
          type: 'category',
          data: [],
          axisTick: {
            alignWithLabel: true
          }
        }
      ],
      yAxis: [
        {
          type: 'value'
        }
      ],
      series: [
        {
          name: 'Direct',
          type: 'bar',
          barWidth: '40%',
          data: []
        }
      ]
    })
  },
  methods: {
    setDay() {
      const day = dayjs().format('YYYY-MM-DD')
      this.date = [day, day]
    },
    setWeek() {
      const start = dayjs().day(1).format('YYYY-MM-DD')
      const end = dayjs().day(7).format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setMonth() {
      const start = dayjs().startOf('month').format('YYYY-MM-DD')
      const end = dayjs().endOf('month').format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setYear() {
      const start = dayjs().startOf('year').format('YYYY-MM-DD')
      const end = dayjs().endOf('year').format('YYYY-MM-DD')
      this.date = [start, end]
    }
  }
}
</script>

<style scoped>
/*>>> .el-card__body {
  padding-top: 0;
}*/

.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}

.tab {
  width: 100%;
}

.right {
  position: absolute;
  right: 0;
}

.right span {
  margin: 0 10px;
}

.date {
  margin: 0 20px;
  width: 240px;
}

.charts {
  width: 100%;
  height: 300px;
}

h3 {
  margin: 0;
}

ul {
  padding: 0;
  width: 100%;
  height: 300px;
  list-style: none;
}

ul li {
  margin: 20px 0;
  height: 8%;
}

ul li span:nth-child(2) {
  margin-left: 40px;
}

.rindex {
  float: left;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background-color: black;
  color: #fff;
  text-align: center;
}

ul li:nth-child(n+4) .rindex {
  color: #000;
  background-color: #eee;
}

.rvalue {
  float: right;
}

</style>
